package com.sumainfor.zuul.controller;

import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.wechat.WxUtil;
import com.sumainfor.zuul.conf.JwtConfiguration;
import com.sumainfor.zuul.entity.SysUserEntity;
import com.sumainfor.zuul.service.ShiroService;
import com.sumainfor.zuul.service.SysUserService;
import com.sumainfor.zuul.util.JwtHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-06-10 16:09
 */
@RestController
@RequestMapping("/wx/system")
public class WxSystemController {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private SysUserService userService;

    @Autowired
    private ShiroService shiroService;

    @Autowired
    private JwtConfiguration configuration;

    @Autowired
    private WxUtil wxUtil;

    /**
     * 登录 获取用户信息 和 token
     *
     * @param code
     * @return
     */
    @PostMapping("/login")
    public JsonResult login(String code) {
        if (StringUtils.isBlank(code)) {
            return JsonResult.errorForEmpty();
        }
        Map<String, Object> configMap = userService.getUnionid(code);
        String unionid = (String) configMap.get("unionid");

        if (StringUtils.isBlank(unionid)) {
            return JsonResult.error("获取UId 失败");
        }

        SysUserEntity user = userService.getUserByUnid(unionid);
        if (user == null) {
            return JsonResult.error("唐巢內部系統，访问请联系管理员！");
        }
        SysUserEntity entity = shiroService.queryUser(user.getUserId());
        CustomMap dataResponse = CustomMap.create("userData", entity)
                .put("token", jwtHelper.generateTokenJwt(String.valueOf(user.getUserId())))
                .put("expire", configuration.getExpire())
                .put("configData", configMap);
        return JsonResult.success(dataResponse);
    }


    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @PostMapping("/getUser")
    public JsonResult getUser(HttpServletRequest request) {
        String header = request.getHeader(configuration.getHead());
        if (StringUtils.isBlank(header)) {
            return JsonResult.error("缺少Token");
        }
        SysUserEntity userEntity = jwtHelper.validateTokenAndGetClaims(header);
        if (userEntity == null) {
            return JsonResult.error("用户不存在");
        }
        return JsonResult.success(userEntity);
    }


    /**
     * 获取签名信息
     *
     * @param url
     * @return
     */
    @PostMapping("/getTicket")
    public JsonResult getTicket(String url) {
        if (StringUtils.isBlank(url)) {
            return JsonResult.errorForEmpty();
        }
        String accessToken = wxUtil.getAccessToken();
        if (StringUtils.isBlank(accessToken)) {
            return JsonResult.error("获取签名失败");
        }

        Map<String, Object> ticket = wxUtil.getTicket(url);
        return JsonResult.success(ticket);
    }

}
